package com.ghn.cocknovel.ui.activity.work

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityToDayWorkTaskBinding
import com.ghn.cocknovel.ext.GONE
import com.ghn.cocknovel.ext.VISIBLE
import com.ghn.cocknovel.ext.pageLoadBase
import com.ghn.cocknovel.ui.activity.map.MapActivity
import com.ghn.cocknovel.ui.activity.work.vm.ToDayWorkTaskViewModel
import com.ghn.cocknovel.view.DialogClickListener
import com.ghn.cocknovel.view.DialogMessage
import com.kt.NetworkModel.bean.ToDayWorkTaskBean
import com.kt.NetworkModel.utils.MLog
import com.tencent.map.fusionlocation.model.TencentGeoLocation
import com.tencent.navix.api.NavigatorZygote
import com.tencent.navix.api.location.GeoLocationObserver
import com.tencent.navix.api.location.LocationApi


/**
 *@author: Administrator
 *2023/10/15
 *desc:今日排班
 */
class ToDayWorkTaskActivity : BaseHeadVMActivity<ToDayWorkTaskViewModel, ActivityToDayWorkTaskBinding, DefaultHeadStyle>() {
    val mWorkAdapter by lazy { WorkAdapter() }
    var locationApi: LocationApi? = null
    lateinit var mLocationObserver: GeoLocationObserver
    var mTencentGeoLocation: TencentGeoLocation? = null
    var mselectPosition = 0
    override fun layoutId(): Int = R.layout.activity_to_day_work_task

    override fun initView(savedInstanceState: Bundle?) {
        setHeadTitle("今日排班", getColor(R.color.colorWhite))
        mHeadStyle.setTitleBackgroundColor(getColor(R.color.color_base_blue))
        mHeadStyle.setLeftImageResource(R.mipmap.ic_back_white)
        mWorkAdapter.bindToRecyclerView(mDatabind.mList)
        mWorkAdapter.setOnLoadMoreListener({
            mViewModel.currentPage++
            mViewModel.driverQueryTransitSchedulePlanListNet()
        }, mDatabind.mList)
    }

    override fun onResume() {
        super.onResume()
        mViewModel.driverQueryTransitSchedulePlanListNet()
    }

    override fun createObserver() {
        mViewModel.mToDayWorkTaskBeanLiveData.observe(this) {
            pageLoadBase(it, mWorkAdapter)
        }
        mViewModel.mOverSubmitLiveData.observe(this) {
            dismissLoading()
            mViewModel.currentPage = 1
            mViewModel.driverQueryTransitSchedulePlanListNet()
            if (it.isNotEmpty()) {
                showMsg(it)
            }
            mWorkAdapter.getItem(mselectPosition).also { workTaskBean ->
                // 跳转到地图
                startActivity(
                    Intent(this@ToDayWorkTaskActivity, MapActivity::class.java)
                        .putExtra("taskId", workTaskBean?.id)
                        .putExtra("fenceAreaId", workTaskBean?.fenceAreaId)
                )
            }

        }
//         var mLocationObserver =GeoLocationObserver() {
//            override fun onGeoLocationChanged(tencentGeoLocation: TencentGeoLocation?) {
//                super.onGeoLocationChanged(tencentGeoLocation)
//                mTencentGeoLocation = tencentGeoLocation
//            }
//        }
        mLocationObserver = object : GeoLocationObserver() {
            override fun onGeoLocationChanged(p0: TencentGeoLocation?) {
                super.onGeoLocationChanged(p0)
                mTencentGeoLocation = p0
            }
        }
        // 通过 NavigatorZygote
        locationApi = NavigatorZygote.with(this).locationApi()
        locationApi?.addLocationObserver(mLocationObserver, 1000)
        //判断当前用户是否打开GPS  没有去打开


    }


    inner class WorkAdapter : BaseQuickAdapter<ToDayWorkTaskBean, BaseViewHolder>, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
        constructor() : super(R.layout.item_to_day_work_task, null) {
            onItemChildClickListener = this
            setOnItemClickListener(this)
        }

        override fun convert(helper: BaseViewHolder, item: ToDayWorkTaskBean) {
            helper.setText(R.id.tvTitle, item.fenceAreaName)
            helper.setText(R.id.tvState, getStateName(item.taskState))
            helper.setText(R.id.tvAddress, item.handoverLocation)
            helper.setText(R.id.tvTime, item.taskStartTime + " - " + item.taskEndTime)
            helper.setText(R.id.tvCarNum, item.carNumber)
            helper.addOnClickListener(R.id.btnStartTask)
            val btnStartTask = helper.getView<TextView>(R.id.btnStartTask)
            when (item.taskState) {
                2 -> {
                    btnStartTask.VISIBLE()
                    btnStartTask.background.setTint(getColor(R.color.color_base_blue))
                    btnStartTask.text = "开始任务"
                }

                3 -> {
                    btnStartTask.VISIBLE()
                    btnStartTask.background.setTint(getColor(R.color.colorRed))
                    btnStartTask.text = "结束任务"
                }

                else -> {
                    helper.getView<View>(R.id.btnStartTask).GONE()
                }
            }

        }

        override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
            try {
                if (mTencentGeoLocation == null) return
                if (mTencentGeoLocation?.location == null) return
                val workTaskBean = adapter.getItem(position) as ToDayWorkTaskBean
                when (workTaskBean.taskState) {
                    2 -> {
                        DialogMessage(this@ToDayWorkTaskActivity).setContext("您是否开始任务").setYesText("开始").setClickListener(object : DialogClickListener() {
                            override fun yesClick(dailog: Dialog?) {
                                mselectPosition = position
                                // 传入经纬度
                                //  dialog 确认后调用
                                showLoading()
                                mViewModel.confirmTransitSchedulePlanNet(workTaskBean.id, mTencentGeoLocation?.location?.longitude.toString(), mTencentGeoLocation?.location?.latitude.toString())
                            }
                        }).show()
                    }

                    3 -> {
                        DialogMessage(this@ToDayWorkTaskActivity).setContext("您是否结束任务").setYesText("结束").setClickListener(object : DialogClickListener() {
                            override fun yesClick(dailog: Dialog?) {
                                showLoading()
                                mViewModel.finishTransitSchedulePlanNet(workTaskBean.id, mTencentGeoLocation?.location?.longitude.toString(), mTencentGeoLocation?.location?.latitude.toString())
                            }
                        }).show()
                    }

                    else -> {

                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }


        override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
            val workTaskBean = adapter.getItem(position) as ToDayWorkTaskBean
            when (workTaskBean.taskState) {
                3 -> {
                    // 跳转到地图
                    startActivity(
                        Intent(this@ToDayWorkTaskActivity, MapActivity::class.java)
                            .putExtra("taskId", workTaskBean.id)
                            .putExtra("fenceAreaId", workTaskBean.fenceAreaId)
                    )
                }
            }
        }

        fun getStateName(state: Int): String {
            //'任务状态 1未开始 2待确认 3进行中 4已结束 5已关闭
            when (state) {
                1 -> return "未开始"
                2 -> return "待确认"
                3 -> return "进行中"
                4 -> return "已结束"
                5 -> return "已关闭"
                else -> {
                    return ""
                }
            }
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        try {
            if (mLocationObserver != null) {
                locationApi?.removeLocationObserver(mLocationObserver)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}